﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Olemas.DataModels;
using Olemas.ViewModels;

namespace Olemas.Controllers
{
    public class AddController : Controller
    {
        //
        // GET: /Add/

        public ActionResult Index()
        {
            var faculties = FacultyModel.GetOrderedFaculties();
            var cities = CityModel.GetCities();
            var addViewModel = new AddViewModel(faculties, cities);
            return View("Index", addViewModel);
        }

        [HttpPost]
        public ActionResult PostBook(IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0 && GlobalViewModel.IsImage(file))
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                    file.SaveAs(path);
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult LoadAddPartial(int id)
        {
            if (id == 1)
            {
                var faculties = FacultyModel.GetOrderedFaculties();
                var cities = CityModel.GetCities();
                var addViewModel = new AddViewModel(faculties, cities);
                return PartialView("_AddBook", addViewModel);
            }
            if (id == 2)
            {
                var faculties = FacultyModel.GetOrderedFaculties();
                var courses = CourseModel.GetCourses();
                var addViewModel = new AddViewModel(faculties, courses);
                return PartialView("_AddConspect", addViewModel);
            }
            if (id == 3)
            {
                var faculties = FacultyModel.GetOrderedFaculties();
                var courses = CourseModel.GetCourses();
                var addViewModel = new AddViewModel(faculties, courses);
                return PartialView("_AddTest", addViewModel);
            }
            return null;
        }

    }
}
